package com.ecy.quadratic.ui.fragment;

import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.ecy.quadratic.R;
import com.ecy.quadratic.adapter.ComicRecommendAdapter;
import com.ecy.quadratic.adapter.ImagePagerAdapter;
import com.ecy.quadratic.beans.ComicClassifyRecommend;
import com.ecy.quadratic.beans.ComicRecommend;
import com.ecy.quadratic.beans.PageInfo;
import com.ecy.quadratic.component.DaggerComicMainComponent;
import com.ecy.quadratic.reader.base.BaseFragment;
import com.ecy.quadratic.reader.component.AppComponent;
import com.ecy.quadratic.ui.activity.ComicRankActivity;
import com.ecy.quadratic.ui.contract.ComicRecommendContract;
import com.ecy.quadratic.ui.presenter.ComicRecommendPresenter;
import com.ecy.quadratic.widget.SwipeRefreshView;
import com.yuyh.easyadapter.glide.GlideRoundTransform;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;


/**
 * Created by Administrator on 2016/3/31.
 */
public class ComicRecommendFragment extends BaseFragment implements ComicRecommendContract.View {

    @Bind(R.id.recommend_swiperefresh_layout)
    SwipeRefreshView mSwipeRefreshView;
    @Bind(R.id.comic_recommend_listview)
    ListView recommendListView;
    AutoScrollViewPager mViewPager;
    View dot_0_view;
    View dot_1_view;
    View dot_2_View;
    View dot_3_View;
    View dot_4_View;
    ImageView mRankFinishedIV;
    ImageView mRankUpdateIV;
    ImageView mRankRankIV;

    @Inject
    ComicRecommendPresenter mRecommendPresenter;

    private int mCurrentPage = 0;
    private int mTotalPage = 0;


    private List<ImageView> images;
    private List<View> dots;
    //记录上一次点的位置
    private int oldPosition = 0;
    private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mRecommendPresenter.getRecommendList("1");
        }
    };

    public ComicRecommendFragment() {

    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_comic_recommend;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerComicMainComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void attachView() {
        if (mRecommendPresenter != null) {
            mRecommendPresenter.attachView(this);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mRecommendPresenter != null)
            mRecommendPresenter.detachView();
    }

    @Override
    public void initData() {

    }

    @Override
    public void configViews() {
        View header = LayoutInflater.from(getContext()).inflate(R.layout.fragment_comic_recommend_header, null);
        mViewPager = (AutoScrollViewPager) header.findViewById(R.id.recommend_top_viewpager);
        dot_0_view = header.findViewById(R.id.dot_0);
        dot_1_view = header.findViewById(R.id.dot_1);
        dot_2_View = header.findViewById(R.id.dot_2);
        dot_3_View = header.findViewById(R.id.dot_3);
        dot_4_View = header.findViewById(R.id.dot_4);
        final ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(R.drawable.c);
        list.add(R.drawable.b);
        list.add(R.drawable.c);
        list.add(R.drawable.d);
        list.add(R.drawable.b);

        mViewPager.setAdapter(new ImagePagerAdapter(getContext(), list).setInfiniteLoop(true));
        mViewPager.setScrollDurationFactor(3);
        mViewPager.setInterval(3000);
        mViewPager.startAutoScroll();
        mViewPager.setCurrentItem(Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % list.size());
        //显示的小点
        dots = new ArrayList<View>();
        dots.add(dot_0_view);
        dots.add(dot_1_view);
        dots.add(dot_2_View);
        dots.add(dot_3_View);
        dots.add(dot_4_View);

        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_MOVE:
                        mSwipeRefreshView.setEnabled(false);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mSwipeRefreshView.setEnabled(true);
                }
                return false;
            }
        });
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {


            @Override
            public void onPageSelected(int position) {
                position = position % list.size();
                dots.get(position).setBackgroundResource(R.drawable.dot_focused);
                dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);

                oldPosition = position;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
        mRankFinishedIV = (ImageView) header.findViewById(R.id.rank_finished_iv);
        mRankRankIV = (ImageView) header.findViewById(R.id.rank_rank_iv);
        mRankUpdateIV = (ImageView) header.findViewById(R.id.rank_update_iv);
        mRankFinishedIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ComicRankActivity.startActivity(getContext());
            }
        });
        mRankRankIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ComicRankActivity.startActivity(getContext());
            }
        });
        mRankUpdateIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ComicRankActivity.startActivity(getContext());
            }
        });
        recommendListView.addHeaderView(header);
        Glide.with(getActivity()).load(R.drawable.rank_finished).centerCrop().transform(new GlideRoundTransform(getActivity(), 10)).into(mRankFinishedIV);
        Glide.with(getActivity()).load(R.drawable.rank_update).centerCrop().transform(new GlideRoundTransform(getActivity(), 10)).into(mRankUpdateIV);
        Glide.with(getActivity()).load(R.drawable.rank_rank).centerCrop().transform(new GlideRoundTransform(getActivity(), 10)).into(mRankRankIV);
        mSwipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mSwipeRefreshView.setColorSchemeResources(R.color.hobby_theme_color, R.color.hobby_theme_color, R.color.hobby_theme_color);
        mSwipeRefreshView.setOnRefreshListener(mOnRefreshListener);
        mSwipeRefreshView.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshView.setRefreshing(true);
                mOnRefreshListener.onRefresh();
            }
        });

//		mSwipeRefreshView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//
//			@Override
//			public void onGlobalLayout() {
//				mSwipeRefreshView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//				mSwipeRefreshView.setRefreshing(true);
//				mOnRefreshListener.onRefresh();
//			}
//		});


    }

    /**
     * 利用线程池定时执行动画轮播
     */
    @Override
    public void onStart() {
        super.onStart();
        mViewPager.startAutoScroll();
    }

    @Override
    public void onStop() {
        super.onStop();
        mViewPager.stopAutoScroll();
    }

    @Override
    public void showRecommendList(PageInfo<ComicRecommend> pageInfo, List<ComicClassifyRecommend> recommends) {
        mSwipeRefreshView.setRefreshing(false);
        mCurrentPage = pageInfo.pageNum;
        mTotalPage = pageInfo.pages;
        recommendListView.setAdapter(new ComicRecommendAdapter(this, recommends));
    }

    @Override
    public void showError() {
        mSwipeRefreshView.setRefreshing(false);
    }

    @Override
    public void complete() {
        mSwipeRefreshView.setRefreshing(false);
    }
}
